home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gximage.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  13.9 KB  |  521 lines

  1. /* Copyright (C) 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gximage.c,v 1.2 2000/09/19 19:00:38 lpd Exp $ */
  20. /* Generic image support */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gscspace.h"
  24. #include "gserrors.h"
  25. #include "gsmatrix.h"
  26. #include "gsutil.h"
  27. #include "gxcolor2.h"        /* for lookup map */
  28. #include "gxiparam.h"
  29. #include "stream.h"
  30.  
  31. /* ---------------- Generic image support ---------------- */
  32.  
  33. /* Structure descriptors */
  34. public_st_gs_image_common();
  35. public_st_gs_data_image();
  36. public_st_gs_pixel_image();
  37.  
  38. /* Initialize the common parts of image structures. */
  39. void
  40. gs_image_common_t_init(gs_image_common_t * pic)
  41. {
  42.     gs_make_identity(&pic->ImageMatrix);
  43. }
  44. void
  45. gs_data_image_t_init(gs_data_image_t * pim, int num_components)
  46. {
  47.     int i;
  48.  
  49.     gs_image_common_t_init((gs_image_common_t *) pim);
  50.     pim->Width = pim->Height = 0;
  51.     pim->BitsPerComponent = 1;
  52.     if (num_components >= 0) {
  53.     for (i = 0; i < num_components * 2; i += 2)
  54.         pim->Decode[i] = 0, pim->Decode[i + 1] = 1;
  55.     } else {
  56.     for (i = 0; i < num_components * -2; i += 2)
  57.         pim->Decode[i] = 1, pim->Decode[i + 1] = 0;
  58.     }
  59.     pim->Interpolate = false;
  60. }
  61. void
  62. gs_pixel_image_t_init(gs_pixel_image_t * pim,
  63.               const gs_color_space * color_space)
  64. {
  65.     int num_components;
  66.  
  67.     if (color_space == 0 ||
  68.     (num_components =
  69.      gs_color_space_num_components(color_space)) < 0
  70.     )
  71.     num_components = 0;
  72.     gs_data_image_t_init((gs_data_image_t *) pim, num_components);
  73.     pim->format = gs_image_format_chunky;
  74.     pim->ColorSpace = color_space;
  75.     pim->CombineWithColor = false;
  76. }
  77.  
  78. /* Initialize the common part of an image-processing enumerator. */
  79. int
  80. gx_image_enum_common_init(gx_image_enum_common_t * piec,
  81.               const gs_data_image_t * pic,
  82.               const gx_image_enum_procs_t * piep,
  83.               gx_device * dev, int num_components,
  84.               gs_image_format_t format)
  85. {
  86.     int bpc = pic->BitsPerComponent;
  87.     int i;
  88.  
  89.     piec->image_type = pic->type;
  90.     piec->procs = piep;
  91.     piec->dev = dev;
  92.     piec->id = gs_next_ids(1);
  93.     switch (format) {
  94.     case gs_image_format_chunky:
  95.         piec->num_planes = 1;
  96.         piec->plane_depths[0] = bpc * num_components;
  97.         break;
  98.     case gs_image_format_component_planar:
  99.         piec->num_planes = num_components;
  100.         for (i = 0; i < num_components; ++i)
  101.         piec->plane_depths[i] = bpc;
  102.         break;
  103.     case gs_image_format_bit_planar:
  104.         piec->num_planes = bpc * num_components;
  105.         for (i = 0; i < piec->num_planes; ++i)
  106.         piec->plane_depths[i] = 1;
  107.         break;
  108.     default:
  109.         return_error(gs_error_rangecheck);
  110.     }
  111.     for (i = 0; i < piec->num_planes; ++i)
  112.     piec->plane_widths[i] = pic->Width;
  113.     return 0;
  114. }
  115.  
  116. /* Compute the source size of an ordinary image with explicit data. */
  117. int
  118. gx_data_image_source_size(const gs_imager_state * pis,
  119.               const gs_image_common_t * pim, gs_int_point * psize)
  120. {
  121.     const gs_data_image_t *pdi = (const gs_data_image_t *)pim;
  122.  
  123.     psize->x = pdi->Width;
  124.     psize->y = pdi->Height;
  125.     return 0;
  126. }
  127.  
  128. /* Process the next piece of an image with no source data. */
  129. /* This procedure should never be called. */
  130. int
  131. gx_no_plane_data(gx_image_enum_common_t * info,
  132.          const gx_image_plane_t * planes, int height,
  133.          int *height_used)
  134. {
  135.     return_error(gs_error_Fatal);
  136. }
  137.  
  138. /* Clean up after processing an image with no source data. */
  139. /* This procedure may be called, but should do nothing. */
  140. int
  141. gx_ignore_end_image(gx_image_enum_common_t * info, bool draw_last)
  142. {
  143.     return 0;
  144. }
  145.  
  146. /* ---------------- Client procedures ---------------- */
  147.  
  148. int
  149. gx_image_data(gx_image_enum_common_t * info, const byte ** plane_data,
  150.           int data_x, uint raster, int height)
  151. {
  152.     int num_planes = info->num_planes;
  153.     gx_image_plane_t planes[gs_image_max_planes];
  154.     int i;
  155.  
  156. #ifdef DEBUG
  157.     if (num_planes > gs_image_max_planes) {
  158.     lprintf2("num_planes=%d > gs_image_max_planes=%d!\n",
  159.          num_planes, gs_image_max_planes);
  160.     return_error(gs_error_Fatal);
  161.     }
  162. #endif
  163.     for (i = 0; i < num_planes; ++i) {
  164.     planes[i].data = plane_data[i];
  165.     planes[i].data_x = data_x;
  166.     planes[i].raster = raster;
  167.     }
  168.     return gx_image_plane_data(info, planes, height);
  169. }
  170.  
  171. int
  172. gx_image_plane_data(gx_image_enum_common_t * info,
  173.             const gx_image_plane_t * planes, int height)
  174. {
  175.     int ignore_rows_used;
  176.  
  177.     return gx_image_plane_data_rows(info, planes, height, &ignore_rows_used);
  178. }
  179.  
  180. int
  181. gx_image_plane_data_rows(gx_image_enum_common_t * info,
  182.              const gx_image_plane_t * planes, int height,
  183.              int *rows_used)
  184. {
  185.     return info->procs->plane_data(info, planes, height, rows_used);
  186. }
  187.  
  188. int
  189. gx_image_flush(gx_image_enum_common_t * info)
  190. {
  191.     int (*flush)(P1(gx_image_enum_common_t *)) = info->procs->flush;
  192.  
  193.     return (flush ? flush(info) : 0);
  194. }
  195.  
  196. bool
  197. gx_image_planes_wanted(const gx_image_enum_common_t *info, byte *wanted)
  198. {
  199.     bool (*planes_wanted)(P2(const gx_image_enum_common_t *, byte *)) =
  200.     info->procs->planes_wanted;
  201.  
  202.     if (planes_wanted)
  203.     return planes_wanted(info, wanted);
  204.     else {
  205.     memset(wanted, 0xff, info->num_planes);
  206.     return true;
  207.     }
  208. }
  209.  
  210. int
  211. gx_image_end(gx_image_enum_common_t * info, bool draw_last)
  212. {
  213.     return info->procs->end_image(info, draw_last);
  214. }
  215.  
  216. /* ---------------- Serialization ---------------- */
  217.  
  218. /*
  219.  * Define dummy sput/sget/release procedures for image types that don't
  220.  * implement these functions.
  221.  */
  222.  
  223. int
  224. gx_image_no_sput(const gs_image_common_t *pic, stream *s,
  225.          const gs_color_space **ppcs)
  226. {
  227.     return_error(gs_error_rangecheck);
  228. }
  229.  
  230. int
  231. gx_image_no_sget(gs_image_common_t *pic, stream *s,
  232.          const gs_color_space *pcs)
  233. {
  234.     return_error(gs_error_rangecheck);
  235. }
  236.  
  237. void
  238. gx_image_default_release(gs_image_common_t *pic, gs_memory_t *mem)
  239. {
  240.     gs_free_object(mem, pic, "gx_image_default_release");
  241. }
  242.  
  243. #ifdef DEBUG
  244. private void
  245. debug_b_print_matrix(const gs_pixel_image_t *pim)
  246. {
  247.     if_debug6('b', "      ImageMatrix=[%g %g %g %g %g %g]\n",
  248.           pim->ImageMatrix.xx, pim->ImageMatrix.xy,
  249.           pim->ImageMatrix.yx, pim->ImageMatrix.yy,
  250.           pim->ImageMatrix.tx, pim->ImageMatrix.ty);
  251. }
  252. private void
  253. debug_b_print_decode(const gs_pixel_image_t *pim, int num_decode)
  254. {
  255.     if (gs_debug_c('b')) {
  256.     const char *str = "      Decode=[";
  257.     int i;
  258.  
  259.     for (i = 0; i < num_decode; str = " ", ++i)
  260.         dprintf2("%s%g", str, pim->Decode[i]);
  261.     dputs("]\n");
  262.     }
  263. }
  264. #else
  265. #  define debug_b_print_matrix(pim) DO_NOTHING
  266. #  define debug_b_print_decode(pim, num_decode) DO_NOTHING
  267. #endif
  268.  
  269. /* Test whether an image has a default ImageMatrix. */
  270. bool
  271. gx_image_matrix_is_default(const gs_data_image_t *pid)
  272. {
  273.     return (is_xxyy(&pid->ImageMatrix) &&
  274.         pid->ImageMatrix.xx == pid->Width &&
  275.         pid->ImageMatrix.yy == -pid->Height &&
  276.         is_fzero(pid->ImageMatrix.tx) &&
  277.         pid->ImageMatrix.ty == pid->Height);
  278. }
  279.  
  280. /* Put a variable-length uint on a stream. */
  281. void
  282. sput_variable_uint(stream *s, uint w)
  283. {
  284.     for (; w > 0x7f; w >>= 7)
  285.     sputc(s, (byte)(w | 0x80));
  286.     sputc(s, w);
  287. }
  288.  
  289. /*
  290.  * Write generic pixel image parameters.  The format is the following,
  291.  * encoded as a variable-length uint in the usual way:
  292.  *    xxxFEDCCBBBBA
  293.  *        A = 0 if standard ImageMatrix, 1 if explicit ImageMatrix
  294.  *        BBBB = BitsPerComponent - 1
  295.  *        CC = format
  296.  *        D = 0 if standard (0..1) Decode, 1 if explicit Decode
  297.  *        E = Interpolate
  298.  *        F = CombineWithColor
  299.  *        xxx = extra information from caller
  300.  */
  301. #define PI_ImageMatrix 0x001
  302. #define PI_BPC_SHIFT 1
  303. #define PI_BPC_MASK 0xf
  304. #define PI_FORMAT_SHIFT 5
  305. #define PI_FORMAT_MASK 0x3
  306. #define PI_Decode 0x080
  307. #define PI_Interpolate 0x100
  308. #define PI_CombineWithColor 0x200
  309. #define PI_BITS 10
  310. /*
  311.  *    Width, encoded as a variable-length uint
  312.  *    Height, encoded ditto
  313.  *    ImageMatrix (if A = 1), per gs_matrix_store/fetch
  314.  *    Decode (if D = 1): blocks of up to 4 components
  315.  *        aabbccdd, where each xx is decoded as:
  316.  *        00 = default, 01 = swapped default,
  317.  *        10 = (0,V), 11 = (U,V)
  318.  *        non-defaulted components (up to 8 floats)
  319.  */
  320. int
  321. gx_pixel_image_sput(const gs_pixel_image_t *pim, stream *s,
  322.             const gs_color_space **ppcs, int extra)
  323. {
  324.     const gs_color_space *pcs = pim->ColorSpace;
  325.     int bpc = pim->BitsPerComponent;
  326.     int num_components = gs_color_space_num_components(pcs);
  327.     int num_decode;
  328.     uint control = extra << PI_BITS;
  329.     float decode_default_1 = 1;
  330.     int i;
  331.     uint ignore;
  332.  
  333.     /* Construct the control word. */
  334.  
  335.     if (!gx_image_matrix_is_default((const gs_data_image_t *)pim))
  336.     control |= PI_ImageMatrix;
  337.     switch (pim->format) {
  338.     case gs_image_format_chunky:
  339.     case gs_image_format_component_planar:
  340.     switch (bpc) {
  341.     case 1: case 2: case 4: case 8: case 12: break;
  342.     default: return_error(gs_error_rangecheck);
  343.     }
  344.     break;
  345.     case gs_image_format_bit_planar:
  346.     if (bpc < 1 || bpc > 8)
  347.         return_error(gs_error_rangecheck);
  348.     }
  349.     control |= (bpc - 1) << PI_BPC_SHIFT;
  350.     control |= pim->format << PI_FORMAT_SHIFT;
  351.     num_decode = num_components * 2;
  352.     if (gs_color_space_get_index(pcs) == gs_color_space_index_Indexed)
  353.     decode_default_1 = pcs->params.indexed.hival;
  354.     for (i = 0; i < num_decode; ++i)
  355.     if (pim->Decode[i] != DECODE_DEFAULT(i, decode_default_1)) {
  356.         control |= PI_Decode;
  357.         break;
  358.     }
  359.     if (pim->Interpolate)
  360.     control |= PI_Interpolate;
  361.     if (pim->CombineWithColor)
  362.     control |= PI_CombineWithColor;
  363.  
  364.     /* Write the encoding on the stream. */
  365.  
  366.     if_debug3('b', "[b]put control=0x%x, Width=%d, Height=%d\n",
  367.           control, pim->Width, pim->Height);
  368.     sput_variable_uint(s, control);
  369.     sput_variable_uint(s, (uint)pim->Width);
  370.     sput_variable_uint(s, (uint)pim->Height);
  371.     if (control & PI_ImageMatrix) {
  372.     debug_b_print_matrix(pim);
  373.     sput_matrix(s, &pim->ImageMatrix);
  374.     }
  375.     if (control & PI_Decode) {
  376.     int i;
  377.     uint dflags = 1;
  378.     float decode[8];
  379.     int di = 0;
  380.  
  381.     debug_b_print_decode(pim, num_decode);
  382.     for (i = 0; i < num_decode; i += 2) {
  383.         float u = pim->Decode[i], v = pim->Decode[i + 1];
  384.         float dv = DECODE_DEFAULT(i + 1, decode_default_1);
  385.  
  386.         if (dflags >= 0x100) {
  387.         sputc(s, dflags & 0xff);
  388.         sputs(s, (const byte *)decode, di * sizeof(float), &ignore);
  389.         dflags = 1;
  390.         di = 0;
  391.         }
  392.         dflags <<= 2;
  393.         if (u == 0 && v == dv)
  394.         DO_NOTHING;
  395.         else if (u == dv && v == 0)
  396.         dflags += 1;
  397.         else {
  398.         if (u != 0) {
  399.             dflags++;
  400.             decode[di++] = u;
  401.         }
  402.         dflags += 2;
  403.         decode[di++] = v;
  404.         }
  405.     }
  406.     sputc(s, (dflags << (8 - num_decode)) & 0xff);
  407.     sputs(s, (const byte *)decode, di * sizeof(float), &ignore);
  408.     }
  409.     *ppcs = pcs;
  410.     return 0;
  411. }
  412.  
  413. /* Set an image's ImageMatrix to the default. */
  414. void
  415. gx_image_matrix_set_default(gs_data_image_t *pid)
  416. {
  417.     pid->ImageMatrix.xx = pid->Width;
  418.     pid->ImageMatrix.xy = 0;
  419.     pid->ImageMatrix.yx = 0;
  420.     pid->ImageMatrix.yy = -pid->Height;
  421.     pid->ImageMatrix.tx = 0;
  422.     pid->ImageMatrix.ty = pid->Height;
  423. }
  424.  
  425. /* Get a variable-length uint from a stream. */
  426. int
  427. sget_variable_uint(stream *s, uint *pw)
  428. {
  429.     uint w = 0;
  430.     int shift = 0;
  431.     int ch;
  432.  
  433.     for (; (ch = sgetc(s)) >= 0x80; shift += 7)
  434.     w += (ch & 0x7f) << shift;
  435.     if (ch < 0)
  436.     return_error(gs_error_ioerror);
  437.     *pw = w + (ch << shift);
  438.     return 0;
  439. }
  440.  
  441. /*
  442.  * Read generic pixel image parameters.
  443.  */
  444. int
  445. gx_pixel_image_sget(gs_pixel_image_t *pim, stream *s,
  446.             const gs_color_space *pcs)
  447. {
  448.     uint control;
  449.     float decode_default_1 = 1;
  450.     int num_components, num_decode;
  451.     int i;
  452.     int code;
  453.     uint ignore;
  454.  
  455.     if ((code = sget_variable_uint(s, &control)) < 0 ||
  456.     (code = sget_variable_uint(s, (uint *)&pim->Width)) < 0 ||
  457.     (code = sget_variable_uint(s, (uint *)&pim->Height)) < 0
  458.     )
  459.     return code;
  460.     if_debug3('b', "[b]get control=0x%x, Width=%d, Height=%d\n",
  461.           control, pim->Width, pim->Height);
  462.     if (control & PI_ImageMatrix) {
  463.     if ((code = sget_matrix(s, &pim->ImageMatrix)) < 0)
  464.         return code;
  465.     debug_b_print_matrix(pim);
  466.     } else
  467.     gx_image_matrix_set_default((gs_data_image_t *)pim);
  468.     pim->BitsPerComponent = ((control >> PI_BPC_SHIFT) & PI_BPC_MASK) + 1;
  469.     pim->format = (control >> PI_FORMAT_SHIFT) & PI_FORMAT_MASK;
  470.     pim->ColorSpace = pcs;
  471.     num_components = gs_color_space_num_components(pcs);
  472.     num_decode = num_components * 2;
  473.     if (gs_color_space_get_index(pcs) == gs_color_space_index_Indexed)
  474.     decode_default_1 = pcs->params.indexed.hival;
  475.     if (control & PI_Decode) {
  476.     uint dflags = 0x10000;
  477.     float *dp = pim->Decode;
  478.  
  479.     for (i = 0; i < num_decode; i += 2, dp += 2, dflags <<= 2) {
  480.         if (dflags >= 0x10000) {
  481.         dflags = sgetc(s) + 0x100;
  482.         if (dflags < 0x100)
  483.             return_error(gs_error_ioerror);
  484.         }
  485.         switch (dflags & 0xc0) {
  486.         case 0x00:
  487.         dp[0] = 0, dp[1] = DECODE_DEFAULT(i + 1, decode_default_1);
  488.         break;
  489.         case 0x40:
  490.         dp[0] = DECODE_DEFAULT(i + 1, decode_default_1), dp[1] = 0;
  491.         break;
  492.         case 0x80:
  493.         dp[0] = 0;
  494.         if (sgets(s, (byte *)(dp + 1), sizeof(float), &ignore) < 0)
  495.             return_error(gs_error_ioerror);
  496.         break;
  497.         case 0xc0:
  498.         if (sgets(s, (byte *)dp, sizeof(float) * 2, &ignore) < 0)
  499.             return_error(gs_error_ioerror);
  500.         break;
  501.         }
  502.     }
  503.     debug_b_print_decode(pim, num_decode);
  504.     } else {
  505.         for (i = 0; i < num_decode; ++i)
  506.         pim->Decode[i] = DECODE_DEFAULT(i, decode_default_1);
  507.     }
  508.     pim->Interpolate = (control & PI_Interpolate) != 0;
  509.     pim->CombineWithColor = (control & PI_CombineWithColor) != 0;
  510.     return control >> PI_BITS;
  511. }
  512.  
  513. /*
  514.  * Release a pixel image object.  Currently this just frees the object.
  515.  */
  516. void
  517. gx_pixel_image_release(gs_pixel_image_t *pic, gs_memory_t *mem)
  518. {
  519.     gx_image_default_release((gs_image_common_t *)pic, mem);
  520. }
  521.